package com.itheima.service.impl;

import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.constants.BehaviorConstants;
import com.itheima.Utils.UserThreadLocal;
import com.itheima.service.BehaviorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author yunfei
 * @date 2022年10月25日 12:01
 */
@Service
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 点赞
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        // 检查参数
        if (dto == null || dto.getArticleId() == null || checkParam(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 点赞  保存数据
        /* 固定前缀+用户ID  {点赞_文章id:1,阅读_文章id:1} */
        if (dto.getOperation() == 0) {
            // 先查询自己是否点赞过
            Boolean hasKey = redisTemplate.opsForHash().hasKey(BehaviorConstants.USER_BEHAVIOR + UserThreadLocal.get(),
                    BehaviorConstants.LIKE_BEHAVIOR + dto.getArticleId());
            if (hasKey) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "已点赞");
            redisTemplate.opsForHash().put(BehaviorConstants.USER_BEHAVIOR + UserThreadLocal.get(),
                    BehaviorConstants.LIKE_BEHAVIOR + dto.getArticleId(), "1");
        } else {
            redisTemplate.opsForHash().delete(BehaviorConstants.USER_BEHAVIOR + UserThreadLocal.get(),
                    BehaviorConstants.LIKE_BEHAVIOR + dto.getArticleId());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 检查参数
     *
     * @return
     */
    private boolean checkParam(LikesBehaviorDto dto) {
        return (dto.getType() > 2 || dto.getType() < 0 || dto.getOperation() > 1 || dto.getOperation() < 0);
    }

    /**
     * 阅读浏览
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        // 检查参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 更新阅读次数
        Boolean hasKey = redisTemplate.opsForHash().hasKey(BehaviorConstants.USER_BEHAVIOR + UserThreadLocal.get(),
                BehaviorConstants.READ_BEHAVIOR + dto.getArticleId());
        if (hasKey) {
            redisTemplate.opsForHash().increment(BehaviorConstants.USER_BEHAVIOR + UserThreadLocal.get(),
                    BehaviorConstants.READ_BEHAVIOR + dto.getArticleId(), 1);
        } else {
            redisTemplate.opsForHash().put(BehaviorConstants.USER_BEHAVIOR + UserThreadLocal.get(),
                    BehaviorConstants.READ_BEHAVIOR + dto.getArticleId(), 1);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 不喜欢
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        // 检查参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 0 不喜欢 1 取消不喜欢
        if (dto.getType() == 0) {
            // 先查询自己是否点不喜欢
            Boolean hasKey = redisTemplate.opsForHash().hasKey(BehaviorConstants.USER_BEHAVIOR + UserThreadLocal.get(),
                    BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId());
            if (hasKey) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "已不喜欢");
            redisTemplate.opsForHash().put(BehaviorConstants.USER_BEHAVIOR + UserThreadLocal.get(),
                    BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId(), "1");
        } else {
            redisTemplate.opsForHash().delete(BehaviorConstants.USER_BEHAVIOR + UserThreadLocal.get(),
                    BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
